import { routerRedux } from 'dva/router';
import { withLoading } from 'utils/dva';
import { breadProvider } from 'utils/bread';
import cookie from 'js-cookie';
import { authProvider } from 'utils/auth';
import services from '../services';
import model from './model';

const ext = ['/login', '/'];

export default model.extend({
    namespace: 'layout',
    state: {
      openKeys: JSON.parse(window.localStorage.getItem('openKeys')) || [],
      user: {},
      loading: {
        login: false,
        password: false
      },
      bread: {
        pathArray: [],
        paramMap: {}
      },
      captcha: '',
      siderFoldN: false,
      messageList: [],
      userInfo: {},
      roleMenuList: {}
    },

    subscriptions: {
      setup({ dispatch, history }) {
        // 监听路由的变化，请求页面数据
        return history.listen(({ pathname }) => {
          if(!ext.includes(pathname)){
            dispatch({ type: 'messageList' });
          }
          dispatch({ type: 'updateBread',  payload: { pathname } });
        });
      }
    },

    effects: {
      * login({ payload }, { put, call, update }) {
        const { data } = yield call(withLoading(services.api.login, 'login'), { ...payload });
        if(data && data.accessToken) {
          cookie.set('token', data.accessToken );
          cookie.set('tokenType', data.tokenType);
          data.name = payload.name;
          const { data: userInfo } =  yield call(withLoading(services.user.userProfile), { ...payload });
          const { data: roleMenuList } =  yield call(withLoading(services.menu.queryRoleMenus), 
          { roleId: userInfo.role.id });
          data.userInfo = userInfo;
          data.role = (userInfo && userInfo.role) || {};
          data.roleMenuList = roleMenuList || [];
          authProvider.init( data.roleMenuList);
          yield put({ type: 'messageList' });
          window.localStorage.setItem('user', JSON.stringify(data));
          yield put(routerRedux.push('/dashboard/monitor'));
        }
      },
      * logout({ payload }, { call, put, update }) {
        cookie.remove('token');
        cookie.remove('tokenType');
        yield put(routerRedux.push('/login'));

      },
      * getMsg({ payload }, { call, update }){
        const { data: captcha } = yield call(withLoading(services.api.getMsg, '', '获取验证码成功'), payload);
        yield update({ captcha });
      },
      * getCaptcha({ payload }, { call, update }){
        const { data: captcha } = yield call(withLoading(services.user.getCaptcha), { ...payload });
        yield update({ captcha });
      },
      * updatePassword({ payload }, { call }){
        yield call(withLoading(services.user.changeMyPassword, 'password', '修改密码成功'), { ...payload });
      },
      * setRead({ payload }, { call }){
        yield call(withLoading(services.api.setRead), { ...payload });
      },
      * setAllRead({ payload }, { call, put }){
        yield call(withLoading(services.api.setAllRead, 'allread', '设置成功'), { ...payload });
        yield put({ type: 'messageList' });
      },
      * messageList({ payload }, { call, update }){
        const { data } =  yield call(withLoading(services.api.messageList, 'messageList'), 
        { ...payload, ps: 5, pn: 1, recStatus: 2, ignoreTips: true });
        yield update({ messageList: data.data });
      },
      * userProfile({ payload }, { call, update }){
        const { data } =  yield call(withLoading(services.user.userProfile), { ...payload });
        yield update({ userInfo: data.data });
      }
    },

    reducers: {
      handleNavOpenKeys(state, { payload: openKeys }) {
        return {
          ...state,
          ...openKeys
        };
      },
      updateBread(state, { payload }) {
        return {
          ...state,
          bread: breadProvider.getBread(payload.pathname)
        };
      },
      switchSider(state, { payload }){
        return {
          ...state,
          siderFoldN: !state.siderFoldN
        };
      }
    }
  });
  